<?php
require_once("libs/general_inc.php");
class Newcategory extends Controller {

    function __construct() {
        parent::__construct();
    }

    function index() {
        $sql = new sql();
        $sql->connect();
        $GENOBJ = new generalFunction();

        authentication();
        $sql = new sql();
        $sql->connect();
        $getPage = getPage();
        $this->view->getPage = $getPage[0];
        $dateTime = DateTime();
        $this->view->template = $sql->fetchAll("template", array("1" => '1'));

        if (isset($_POST['delete'])) {
            for ($i = 0; $i < count($_POST['cat_id']); $i++) {
                
                $arraydata = array(
                    "cat_status" => 2,
                );
                //$update=$sql->update('page',array("permalink"=>$permalink),array("uid"=>$_POST['editUid']));
                $update = $sql->update('tbl_category', $arraydata, array("cat_id" => $_POST['cat_id'][$i]));
                //$delete = $sql->delete('tbl_category', array("cat_id" => $_POST['cat_id'][$i]));
            }
            if ($update) {
                header("Location: " . URL . "newcategory/?action=view&reply=NewcategoryDeleted");
            } else {
                header("Location: " . URL . "newcategory/?action=view&reply=PageError");
            }
        }

        if (GET('action') == 'add') {
            if (isset($_POST['submit'])) {
                
                $arraydata = array("cat_name" => $_POST['cat_name'],
                    "cat_status" => $_POST['cat_status'],
                    "cat_alias" => str_replace(" ", "-", $_POST['cat_name']),
                    "cat_createdate" => date('Y-m-d h:i:s'),
                    "cat_updatedate" => date('Y-m-d h:i:s'),
                    "LastModifyBy" => base64_decode($_COOKIE['login_user_uid']),
                );
                $filterArray = Array("TrimElement", "htmlEntity");  
                $filterElements = $GENOBJ->filter($arraydata, $filterArray);
                $insert = $sql->insert('tbl_category', $filterElements);
                if ($insert) {
                    header("Location: " . URL . "newcategory/?action=view&reply=NewcategoryCreated");
                } else {
                    header("Location: " . URL . "newcategory/?action=view&reply=NewcategoryError");
                }
            }   
        }

        if (isset($_POST['submit']) and isset($_POST['editUid'])) {

            $arraydata = array("cat_name" => $_POST['cat_name'],
                "cat_status" => $_POST['cat_status'],
                "cat_alias" => str_replace(" ", "-", $_POST['cat_name']),
                "cat_updatedate" => date('Y-m-d h:i:s'),
                "LastModifyBy" => base64_decode($_COOKIE['login_user_uid']),
            );

           $filterArray = Array("TrimElement", "htmlEntity");  
           $filterElements = $GENOBJ->filter($arraydata, $filterArray);
                
            //$update=$sql->update('page',array("permalink"=>$permalink),array("uid"=>$_POST['editUid']));
            $update = $sql->update('tbl_category', $filterElements, array("cat_id" => $_POST['editUid']));
            if ($update) {
                header("Location: " . URL . "newcategory/?action=view&reply=NewcategoryUpdated");
            } else {
                header("Location: " . URL . "newcategory/?action=view&reply=NewcategoryError");
            }
        }



        if (GET('action') == 'view') {
            $page = (int) (GET("page") ? GET("page") : 1 );
            if (GET('search')) {
                $statement = "tbl_category where cat_status!=2 and  cat_name like '%" . GET('q') . "%' order by cat_id desc";
                $this->view->oldData = "action=" . GET('action') . "&q=" . GET('q') . "search=" . GET('q');
            } else {
                $statement = "tbl_category where cat_status!=2 order by cat_id desc";
                $this->view->oldData = "action=" . GET('action');
            }
            $limit = 50;
            $this->view->statement = $statement;
            $this->view->limit = $limit;
            $this->view->page = $page;
            $this->view->PagingNationFetch = $sql->PagingNationFetch($statement, $page, $limit);
        } 
        elseif (GET('action') == 'edit') {
            $this->view->editrow = $sql->fetch("tbl_category", array("cat_id" => GET('editUid')));

        }


        if (GET('reply')) {
            $this->view->noti = msg(GET('reply'));
        }

        if (checkPermission('premission_category')) {
            $this->view->render('newcategory');
        } else {
            $this->view->render('permission_denied');
        }
    }

}

